﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.utils;
using l1cs.server.datatables;

namespace l1cs.server.model
{
    public class Beginner
    {
        private static Logger _log = Logger.getLogger( typeof( Beginner ).FullName );

        private static Beginner _instance;

        public static Beginner getInstance()
        {
            if ( _instance == null )
            {
                _instance = new Beginner();
            }
            return _instance;
        }

        private Beginner()
        {
        }

        /// <summary>
        /// 初心者アイテム支給メソッド
        /// </summary>
        /// <param name="pc">PCインスタンス</param>
        /// <returns></returns>
        public int GiveItem( L1PcInstance pc )
        {
            String data1 = "";
            String data2 = "";
            data1 = "A";
            if ( pc.isCrown() )
            {
                data2 = "P";
            }
            else if ( pc.isKnight() )
            {
                data2 = "K";
            }
            else if ( pc.isElf() )
            {
                data2 = "E";
            }
            else if ( pc.isWizard() )
            {
                data2 = "W";
            }
            else if ( pc.isDarkelf() )
            {
                data2 = "D";
            }
            else if ( pc.isDragonKnight() )
            { // ドラゴンナイト
                data2 = "R";
            }
            else if ( pc.isIllusionist() )
            { // イリュージョニスト
                data2 = "I";
            }
            else
            {
                data2 = "A";// 万が一どれでもなかった場合のエラー回避用
            }

            String sqlstr = "SELECT * FROM beginner WHERE activate IN(?1,?2)";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand(sqlstr);
                cmd.Parameters.Add( new MySqlParameter( "?1", data1 ) );
                cmd.Parameters.Add( new MySqlParameter( "?2", data2 ) );
                DataTable dt = wrapper.GetDataTable( cmd );
                foreach ( DataRow item in dt.Rows )
                {
                    sqlstr = @"INSERT INTO character_items SET 
                             id            =?1, 
                             item_id       =?2, 
                             char_id       =?3, 
                             item_name     =?4, 
                             count         =?5, 
                             is_equipped   =?6, 
                             enchantlvl    =?7, 
                             is_id         =?8, 
                             durability    =?9, 
                             charge_count  =?10, 
                             remaining_time=?11, 
                             last_used     =?12, 
                             bless         =?13";
                    MySqlCommand cmdins = new MySqlCommand( sqlstr );
                    cmdins.Parameters.Add( new MySqlParameter( "?1", IdFactory.getInstance().nextId() ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?2", SqlUtils.toInt( item["item_id"] ) ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?3", pc.getId() ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?4", Convert.ToString( item["item_name"] ) ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?5", SqlUtils.toInt( item["count"] ) ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?6", (Object)0 ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?7", SqlUtils.toInt( item["enchantlvl"] ) ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?8", (Object)0 ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?9", (Object)0 ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?10", SqlUtils.toInt( item["charge_count"] ) ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?11", (Object)0 ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?12", null ) );
                    cmdins.Parameters.Add( new MySqlParameter( "?13", (Object)1 ) );
                    wrapper.ExecNonQuery( cmdins );
                }
            }
            return 0;
        }
    }
}
